﻿using Model.Core;
using Newtonsoft.Json;
using SIE.SCADA.Common.Log;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace SIE.SCADA.StationDemo
{
    class Env
    {
        public static Dictionary<string, OrderMsg> v_Scada_Processrecords;
        public static bool WorkState { get; set; }
        public static StationConfig StationConfig { get; set; }
        private static string _configFilePath;

        static Env()
        {
            Init();
        }

        static void Init()
        {
            v_Scada_Processrecords = new Dictionary<string, OrderMsg>();

            System.IO.DirectoryInfo topDir = System.IO.Directory.GetParent(Assembly.GetExecutingAssembly().Location);
            var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);//获取当前dll所在文件夹
            _configFilePath = Path.Combine(dir, "StationConfig.config");
            if (!File.Exists(_configFilePath))
            {
                _configFilePath = Path.Combine(topDir.Parent.FullName, "StationConfig.config");
                if (!File.Exists(_configFilePath))
                {
                    Logger.Warn($"服务端：配置文件不存在（{_configFilePath}）");
                    return;
                }
            }
            Load(_configFilePath);//加载配置文件
            return;
        }

        static bool Load(string fileName)
        {
            if (!File.Exists(fileName)) return false;
            string text = System.IO.File.ReadAllText(fileName);
            StationConfig = JsonConvert.DeserializeObject<StationConfig>(text);
            return true;
        }
    }

  
}
